const axios = require('axios');
const { stat } = require('fs');
const { 获取输入, 获取输入_plus, 文本取中间 } = require('lidong-utils-zh');
const qs = require('qs');
const sm2 = require('sm-crypto').sm2

const getGeocoder = async ({ lon, lat }) => {
    let config = {
        method: 'GET',
        url: `https://api.tianditu.gov.cn/apiserver/ajaxproxy?proxyReqUrl=https://api.tianditu.gov.cn/geocoder?postStr={"lon":${lon},"lat":${lat},"appkey":"8a7b9aac0db21f9dd995e61a14685f05","ver":"1"}&type=geocode&tk=46f92187d51cafd01013ac3474f4ba48`,
        headers: {
            'User-Agent': 'Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36',
            'Accept-Encoding': 'gzip, deflate, br, zstd',
            'Pragma': 'no-cache',
            'Cache-Control': 'no-cache',
            'sec-ch-ua-platform': '"Android"',
            'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
            'sec-ch-ua-mobile': '?1',
            'Sec-Fetch-Site': 'cross-site',
            'Sec-Fetch-Mode': 'no-cors',
            'Sec-Fetch-Dest': 'script',
            'Sec-Fetch-Storage-Access': 'active',
            'Referer': 'https://gm-gd.i-aq.cn/',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        }
    };

    const result = (await axios.request(config)).data
    return result
}

// (学生10签到 30签退)(老师teacherSign签到)
const checkInOrOut = async ({ trId, ssoToken, signType, lon, lat, address, loginType }) => {

    // 10签到 30签退
    let data = qs.stringify({
        'trId': trId,
        'signType': signType,
        'lon': lon,
        'lat': lat,
        'address': address
    });

    if (loginType == 1) {
        data = qs.stringify({
            'trId': trId,
            'signType': "teacherSign",
            'lon': lon,
            'lat': lat,
            'address': address
        });
    }

    let config = {
        method: 'POST',
        url: 'https://pxjg-gd.i-aq.cn/pxjgApi/student/studentTrainSign/sign',
        headers: {
            'User-Agent': 'Mozilla/5.0 (Linux; Android 12; PGT-AN00 Build/V417IR) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.120 Mobile Safari/537.36',
            'Accept-Encoding': 'gzip, deflate',
            'Content-Type': 'application/x-www-form-urlencoded',
            'sso-token': ssoToken,
            'project-code': 'gdpxjg',
            'product-code': 'pxjgApp',
            'token-version': 'V2.1',
            'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
            'Origin': 'https://gm-gd.i-aq.cn',
            'X-Requested-With': 'com.android.browser',
            'Sec-Fetch-Site': 'same-site',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Referer': 'https://gm-gd.i-aq.cn/',
            'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7'
        },
        data: data
    };

    const result = (await axios.request(config)).data

    return result

}

const test = async () => {


    const trId = await 获取输入_plus({
        prompt: "请输入trId：",
        key: "trId"
    })
    const ssoToken = await 获取输入_plus({
        prompt: "请输入ssoToken：",
        key: "ssoToken"
    })

    const signType = await 获取输入_plus({
        prompt: "请输入signType：(学生10签到 20续签 30签退)(老师teacherSign签到)",
        key: "signType"
    })


    const lon = await 获取输入_plus({
        prompt: "请输入精度：",
        key: "lon"
    })

    const lat = await 获取输入_plus({
        prompt: "请输入维度：",
        key: "lat"
    })

    let geoResult = await getGeocoder({
        lon,
        lat
    })

    geoResult = 文本取中间(geoResult, 'loadResult=', ';')

    geoResult = JSON.parse(geoResult)

    const checkInResult = await checkInOrOut({
        trId,
        ssoToken,
        signType,
        lon: geoResult.result.location.lon,
        lat: geoResult.result.location.lat,
        address: geoResult.result.formatted_address
    })
    console.log(JSON.stringify({
        status: checkInResult.status,
        message: checkInResult.message,
        address: geoResult.result.formatted_address,
        lon: geoResult.result.location.lon,
        lat: geoResult.result.location.lat,
    }));
}
test()